// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import axios from 'axios';
import qs from "qs";
import md5 from 'js-md5';
import store from "./store"
import Base64  from 'base-64';
import * as echarts from 'echarts';
//自适应
import 'lib-flexible/flexible';
import './utils/flexible'
// 懒加载
import VueLazyLoad from 'vue-lazyload'


Vue.use(VueLazyLoad,{

})
// mock
require('./mock.js'); //引入mock数据，关闭则注释该行
// UI
import {
  Pagination,
  Dialog,
  Menu,
  Submenu,
  MenuItem,
  MenuItemGroup,
  Input,
  InputNumber,
  Radio,
  RadioGroup,
  RadioButton,
  Checkbox,
  CheckboxButton,
  CheckboxGroup,
  Switch,
  Select,
  Option,
  OptionGroup,
  Button,
  ButtonGroup,
  Table,
  TableColumn,
  DatePicker,
  TimeSelect,
  TimePicker,
  Popover,
  Tooltip,
  Breadcrumb,
  BreadcrumbItem,
  Form,
  FormItem,
  Tabs,
  TabPane,
  Tag,
  Tree,
  Slider,
  Icon,
  Row,
  Col,
  Upload,
  Spinner,
  Badge,
  Card,
  Carousel,
  CarouselItem,
  Container,
  Header,
  Aside,
  Main,
  Footer,
  Timeline,
  TimelineItem,
  Link,
  Divider,
  Loading,
  MessageBox,
  Message,
} from 'element-ui';
import CollapseTransition from 'element-ui/lib/transitions/collapse-transition'

Vue.use(Pagination);
Vue.use(Dialog);
Vue.use(Menu);
Vue.use(Submenu);
Vue.use(MenuItem);
Vue.use(MenuItemGroup);
Vue.use(Input);
Vue.use(InputNumber);
Vue.use(Radio);
Vue.use(RadioGroup);
Vue.use(RadioButton);
Vue.use(Checkbox);
Vue.use(CheckboxButton);
Vue.use(CheckboxGroup);
Vue.use(Switch);
Vue.use(Select);
Vue.use(Option);
Vue.use(OptionGroup);
Vue.use(Button);
Vue.use(ButtonGroup);
Vue.use(Table);
Vue.use(TableColumn);
Vue.use(DatePicker);
Vue.use(TimeSelect);
Vue.use(TimePicker);
Vue.use(Popover);
Vue.use(Tooltip);
Vue.use(Breadcrumb);
Vue.use(BreadcrumbItem);
Vue.use(Form);
Vue.use(FormItem);
Vue.use(Tabs);
Vue.use(TabPane);
Vue.use(Tag);
Vue.use(Tree);
Vue.use(Slider);
Vue.use(Icon);
Vue.use(Row);
Vue.use(Col);
Vue.use(Upload);
Vue.use(Spinner);
Vue.use(Badge);
Vue.use(Card);
Vue.use(Carousel);
Vue.use(CarouselItem);
Vue.use(Container);
Vue.use(Header);
Vue.use(Aside);
Vue.use(Main);
Vue.use(Footer);
Vue.use(Timeline);
Vue.use(TimelineItem);
Vue.use(Link);
Vue.use(Divider);


// 全局方法及变量
import publicAPI from "./public/public.js"
Vue.prototype.$publicAPI = publicAPI;

Vue.prototype.$loading = Loading.service;
Vue.prototype.$msgbox = MessageBox;
Vue.prototype.$alert = MessageBox.alert;
Vue.prototype.$confirm = MessageBox.confirm;
Vue.prototype.$prompt = MessageBox.prompt;
Vue.prototype.$message = Message;

Vue.component(CollapseTransition.name, CollapseTransition)


// 设置反向代理 前端请求默认发送到
axios.defaults.baseURL = "http://192.168.124.131:8318"

Vue.config.productionTip = false
// 全局注册 之后可以在其他组件中通过this.$axios发送shuju
// 类似于全局事件总线
Vue.prototype.$qs = qs;
Vue.prototype.$echarts = echarts;

axios.defaults.withCredentials = false
Vue.prototype.$axios = axios;
Vue.prototype.$md5 = md5;

router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) {  // 判断该路由是否需要登录权限
    if (sessionStorage.tokenvalue) {  // 获取当前的token是否存在
      console.log("token存在");
      if(to.path === '/manager/welcome' || to.path === '/manager/user' || to.path === '/manager/manage' || to.path === '/manager/rights' || to.path === '/manager/roles' || to.path === '/manager/news' || to.path === '/manager/hotMajor' || to.path === '/manager/searchMajor' || to.path === '/manager/horCollege' || to.path === '/manager/searchCollege' || to.path === '/manager/exceptionLog' || to.path === '/manager/operationLog'){
        if(sessionStorage.identify === "201"){
          next();
          console.log(sessionStorage.identify);
        }else{
          console.log(sessionStorage.identify);
          Message({
            title: '警告',
            message: '您无权进入后台！！',
            type: 'error'
          });
          next("/");
        }
      }else{
        next();
      }
    } else {
      let _this = this;
      console.log("token不存在");
      // alert("请先登录");
      Message({
        title: '警告',
        message: '请先登录！！！',
        type: 'warning'
      });
      next({
        path: '/login', // 将跳转的路由path作为参数，登录成功后跳转到该路由
      })
    }
  }else { // 如果不需要权限校验，直接进入路由界面
    next();
  }
});


// 请求拦截器
axios.interceptors.request.use(config => {
  let pathname = location.pathname;
  if(pathname !== '/login'){
      if(sessionStorage.getItem("tokenValue")){
        if( pathname !== '/college' && pathname !== '/major' && pathname !== '/news'){
          config.headers.common["token"] = sessionStorage.getItem("tokenValue");
        }
    }
  }
  return config;
},error =>{
  return Promise.reject(error);
})

// 相应拦截器
axios.interceptors.response.use(response=>{
  return response;
},error=>{
  if(error.response){
    switch(error.response.status){
      case 899:
        sessionStorage.removeItem("tokenValue");
        router.replace({
          path:"/error",
        });
      case 789:
        sessionStorage.removeItem("tokenValue");
        router.replace({
          path:"/login",
        });
    }
    return Promise.reject(error.response.data);
  }
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>',
  beforeCreate() {
		Vue.prototype.$bus = this
	},
})




